package com.cungmua.app.base;

import android.content.Intent;
import android.support.v4.app.ListFragment;

import com.cungmua.app.activity.AboutActivity;
import com.cungmua.app.activity.AccountPreminumActivity;
import com.cungmua.app.activity.BonusActivity;
import com.cungmua.app.activity.CategoryActivity;
import com.cungmua.app.activity.ListDealActivity;
import com.cungmua.app.activity.LoginActivity;
import com.cungmua.app.activity.OrderHistoryActivity;
import com.cungmua.app.activity.PasswordChangeActivity;
import com.cungmua.app.activity.ProfileActivity;
import com.cungmua.app.activity.RegisterActivity;
import com.cungmua.app.activity.SearchResultsActivity;
import com.cungmua.app.model.Cart;
import com.cungmua.app.model.City;
import com.cungmua.app.model.User;
import com.cungmua.app.utils.JsonUtils;
import com.cungmua.app.utils.PrefUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;

/**
 * Created by kelvin.nguyen on 6/20/14.
 */
public class BaseActivityScreen extends ListFragment {
    public static final String IK_CATEGORY_ID = "IK_CATEGORY_ID";
    public static final String IK_TYPE_CONTENT = "IK_TYPE_CONTENT";
    public static final String CITY_KEY = "city";
    public static final String DEALTYPE = "dealType";
    private boolean isBack=false;
    public static final String IK_USER = "IK_USER";
    public static final String USER_KEY = "user";
    public static final String CART_KEY = "cart";
    public static final String KEY_SEARCH = "keyword";
    public static final int REQUEST_REGISTER = 5;
    public static final int REQUEST_CHANGE_PASS = 7;
    public static final int HUUICH_ABOUT=1;
    public static final int HUUICH_HINHTHUC=5;
    public static final int HUUICH_HINHTHUC_GIAO=6;
    public static final int HUUICH_HUONGDAN=4;
    public static final int HUUICH_TRAHANG=3;
    public static final int HUUICH_QUYCHE=2;


    public static final int THONGTIN= 11111;
    public static final int THONGTIN_TAIKHOAN= 1212121212;
    public static final int THONGTIN_HUUICH= 1313131313;
    public static final int SELECT_CITY= 1515151515;
    public static final int DEFAULT_CITY= 1616161616;
    public static final int LOGIN= 1717171717;
    public static final int DANHMUC=33333 ;
    public static final int HOME= 1;
    public static final int PROMOTION=2 ;

    public static final String IK_METHOD_YAHOO = "IK_METHOD_YAHOO";
    public static final String IK_METHOD_GOOGLE = "IK_METHOD_GOOGLE";
    public static final int REQUEST_EDIT_PROFILE = 6;
    public static final int REQUEST_LOGIN = 1;
    public static final String LOGIN_REQUEST_KEY = "login_request";



    public boolean isBack() {
        return isBack;
    }

    public void setBack(boolean isBack) {
        this.isBack = isBack;
    }


    public void goToHotDeal(boolean clear,int dealType) {
        Intent intent = new Intent(getActivity(), ListDealActivity.class);
        intent.putExtra(DEALTYPE,dealType);
        if (clear)
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

    public void goToCategory(int categoryId, String name) {
        Intent intent = new Intent(getActivity(), CategoryActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("EXIT", true);
        intent.putExtra(IK_CATEGORY_ID, categoryId);
        intent.putExtra("TITLE",name);
        startActivity(intent);
    }

    public void goToSearch(String keyword) {
        Intent intent = new Intent(getActivity(), SearchResultsActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("EXIT", true);
        intent.putExtra(KEY_SEARCH, keyword);
        startActivity(intent);
    }

    public void logOut() {
        User currentUser = getCurrentUser();
        currentUser.setId(0);
        setCurrentUser(currentUser);
        setCurrentCart(new Cart());
        getActivity().finish();
        goToHotDeal(true,0);
        //refreshActivity();
    }

    public void setCurrentUser(User user) {
        ObjectMapper mapper = JsonUtils.defaultMapper();
        try {
            PrefUtils.savePreferences(getActivity(), USER_KEY, mapper.writeValueAsString(user));
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }


    public void login() {
        Intent intent = new Intent(getActivity(), LoginActivity.class);
        startActivityForResult(intent, REQUEST_LOGIN);
    }
    public void register() {
        Intent intent = new Intent(getActivity(), RegisterActivity.class);
        startActivityForResult(intent, REQUEST_REGISTER);
    }
    public void goToProfile() {
        Intent intent = new Intent(getActivity(), ProfileActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("EXIT", true);
        startActivity(intent);
    }

    public void goToPastDeals() {
        Intent intent = new Intent(getActivity(), OrderHistoryActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("EXIT", true);
        startActivity(intent);
    }

    public void goToBonus() {
        Intent intent = new Intent(getActivity(), BonusActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("EXIT", true);
        startActivity(intent);
    }

    public void goToAbout(int typeContent) {
        Intent intent = new Intent(getActivity(), AboutActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("EXIT", true);
        intent.putExtra(IK_TYPE_CONTENT,typeContent);
        startActivity(intent);
    }

    public void goToAccountPremium() {
        Intent intent = new Intent(getActivity(), AccountPreminumActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("EXIT", true);
        startActivity(intent);
    }



    public void changePassword() {
        Intent intent = new Intent(getActivity(), PasswordChangeActivity.class);
        startActivityForResult(intent, REQUEST_CHANGE_PASS);
    }


    public User getCurrentUser() {
        User currentUser;
        ObjectMapper mapper = JsonUtils.defaultMapper();
        try {
            String json = PrefUtils.readPreferences(getActivity(), USER_KEY, "");
            currentUser = mapper.readValue(json, User.class);
        } catch (IOException e) {
            e.printStackTrace();
            return User.defaultUser();
        }
        if (currentUser.getToken() == null)
            return User.defaultUser();

        return currentUser;
    }

    public void setCurrentCart(Cart cart) {
        ObjectMapper mapper = JsonUtils.defaultMapper();
        try {
            PrefUtils.savePreferences(getActivity(), CART_KEY, mapper.writeValueAsString(cart));
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
    public City getCurrentCity() {
        City currentCity;
        ObjectMapper mapper = JsonUtils.defaultMapper();
        try {
            String json = PrefUtils.readPreferences(getActivity(), CITY_KEY, "");
            currentCity = mapper.readValue(json, City.class);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }

        return currentCity;
    }


    public void setCurrentCity(City city) {

        if(city.getId()!=getCurrentCity().getId()){
            setCurrentCart(new Cart());
        }

        ObjectMapper mapper = JsonUtils.defaultMapper();
        try {
            PrefUtils.savePreferences(getActivity(), CITY_KEY, mapper.writeValueAsString(city));
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }


}
